<template>
  <div class="menu_list">
    <!-- 按钮 -->
    <div class="btns">
      <el-button type="primary" size="small" @click="toSaveHandler"
        >新增</el-button
      >
    </div>
    <!-- 按钮 -->
    <!-- 表格 -->
    <el-table
      size="small"
      :data="categoryData.list"
      v-loading="loading"
      row-key="id"
      :tree-props="{ children: 'children' }"
    >
      <el-table-column label="栏目图标" prop="icon" align="center">
        <template slot-scope="scope">
          <img
            :src="scope.row.icon"
            alt=""
            style="width: 30px; cursor: pointer"
            @click="toPhotoHandler(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="名称"
        prop="name"
        align="center"
      ></el-table-column>
      <el-table-column label="编号" prop="num" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="toDeleteHandler(scope.row)"
            >删除</el-button
          >
          <el-button type="text" size="mini" @click="toEditHandler(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格 -->
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      small
      :hide-on-single-page="true"
      :total="categoryData.total"
      :page-size="categoryData.pageSize"
      :current-page="categoryData.page"
      @current-change="currentChangeHandler"
    >
    </el-pagination>
    <!-- 分页 -->
    <!-- 模态框 -->
    <el-dialog
      :title="title"
      :visible.sync="visible"
      width="50%"
      @close="clearValidate"
    >
      <div>
        <el-form
          :model="form"
          ref="categoryForm"
          :rules="rules"
          label-width="80px"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="序号" prop="num">
            <el-input v-model="form.num"></el-input>
          </el-form-item>
          <!-- <el-form-item label="一级菜单" prop="parentId">
            <el-select v-model="form.parentId">
              <el-option
                v-for="c in categoryData.list"
                :label="c.name"
                :value="c.id"
                :key="c.id"
              ></el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="栏目图标" prop="icon">
            <el-upload
              ref="upload"
              class="upload-demo"
              :action="this.baseURL + '/file/upload'"
              :on-success="uploadSuccessHandler"
              :file-list="fileList"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">文件大小不允许超过3M</div>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取消</el-button>
        <el-button size="small" type="primary" @click="submitHandler"
          >确定</el-button
        >
      </span>
    </el-dialog>
    <!-- 模态框 -->
  </div>
</template>
<script>
import { get, post } from "@/utils/request";
import _ from "lodash";
export default {
  data() {
    return {
      visible: false,
      loading: false,
      title: "新增菜品分类信息",
      fileList: [],
      param: {
        page: 1,
        pageSize: 15,
      },
      categoryData: {
        list: [],
      },
      form: {},
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
    };
  },
  created() {
    this.loadCategorys();
  },
  methods: {
    submitHandler() {
      // console.log(this.$refs["categoryForm"]);
      this.$refs["categoryForm"].validate((valid) => {
        if (valid) {
          let url = "/productCategory/saveOrUpdate";
          post(url, this.form).then((response) => {
            this.$message({ type: "success", message: response.message });
            this.loadCategorys();
            this.visible = false;
          });
        } else {
          return false;
        }
      });
    },
    // 上传成功
    uploadSuccessHandler(response) {
      if (response.status == 200) {
        this.$set(
          this.form,
          "icon",
          "http://121.199.29.84:8888/group1/" + response.data.id
        );
      } else {
        this.$message({ type: "error", message: "附件服务器异常！" });
        this.visible = false;
      }
    },
    // 去新增
    toSaveHandler() {
      this.title = "新增菜品分类信息";
      this.form = {};
      this.visible = true;
    },
    // 去修改
    toEditHandler(row) {
      this.title = "修改菜品分类信息";
      this.form = _.clone(row);
      this.visible = true;
    },
    // 去删除
    toDeleteHandler(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let url = "/productCategory/deleteById";
        get(url, { id: row.id }).then((response) => {
          this.$message({ type: "success", message: "删除成功!" });
          this.loadCategorys();
        });
      });
    },
    // 查询
    loadCategorys() {
      this.loading = true;
      let url = "/productCategory/pageQuery";
      get(url, this.param).then((response) => {
        this.categoryData = response.data;
        this.loading = false;
      });
    },
    // 当前页发生改变
    currentChangeHandler(page) {
      this.param.page = page;
      this.loadCategorys();
    },
    // 重置表单校验
    clearValidate() {
      this.$refs["categoryForm"].clearValidate();
      this.$refs["upload"].clearFiles();
    },
  },
};
</script>
